/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.fvaz.demo.servlets;

import fr.fvaz.demo.utils.App;
import fr.fvaz.demo.utils.ConfigurationReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author felipe
 */
public class Handler extends HttpServlet {

    private static final String URIConfigurationFile = "/WEB-INF/apps.xml";
    private static final String startPath = "app/do";

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {
            
            int startIndex = request.getRequestURI().indexOf(startPath);
            int stopIndex = request.getRequestURI().indexOf("/", startIndex + 4);
            stopIndex = stopIndex < 0 ? request.getRequestURI().length() : stopIndex;

            String appName = request.getRequestURI().substring(startIndex, stopIndex).replaceFirst(startPath, "");

            App app = ConfigurationReader.getConfiguration(getServletContext().getRealPath(URIConfigurationFile), appName);

            if (app != null) {

                Class[] paramTypes = {HttpServletRequest.class, HttpServletResponse.class};
                Object[] args = { request, response};
                
                Object validator = Class.forName(app.getClassName()).newInstance();
                Method methode = validator.getClass().getMethod("execute", paramTypes);
                methode.invoke(validator, args);
            }

            getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
        
        } catch (IllegalArgumentException ex) {
            
            ex.printStackTrace();
            
        } catch (InvocationTargetException ex) {
            
            ex.printStackTrace();
            
        } catch (NoSuchMethodException ex) {
            
            ex.printStackTrace();
            
        } catch (SecurityException ex) {
            
            ex.printStackTrace();
            
        } catch (ClassNotFoundException ex) {
            
            ex.printStackTrace();
            
        } catch (InstantiationException ex) {
            
            ex.printStackTrace();
            
        } catch (IllegalAccessException ex) {
            
            ex.printStackTrace();
            
        } catch( IOException ex){
        
            ex.printStackTrace();
            
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    //public abstract void execute(HttpServletRequest request, HttpServletResponse response);
}
